//
//  SDCheckButton.m
//  Sudoku
//
//  Created by 兰伟欣 on 16/6/13.
//  Copyright © 2016年 wxlan. All rights reserved.
//

#import "SDCheckButton.h"
@interface SDCheckButton()

@property (assign, nonatomic) CGFloat buttonWidth;
@end

@implementation SDCheckButton

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
//- (instancetype)init
//{
//    if (self = [super init]) {
//        [self setup];
//    }
//    return self;
//}
- (instancetype)initWithWidth:(CGFloat)width
{
        if (self = [super init]) {
            self.buttonWidth = width;
            [self setupWithWidth:width];
        }
        return self;

}

// 按钮初始化
- (void)setupWithWidth:(CGFloat)width
{
    CGFloat childLabelWidth = (width-5)/3;
    for (int i = 0; i < 9; i++) {
        UILabel *label = [[UILabel alloc] init];
        [self addSubview:label];
        label.font = [UIFont systemFontOfSize:10];
        label.hidden = YES;
        label.text = [NSString stringWithFormat:@"%d", i+1];
        label.tag = i+1;
        
        
        NSInteger row = i/3;
        NSInteger column = i%3;
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.equalTo(@(childLabelWidth));
            make.height.equalTo(@(childLabelWidth));
            make.left.equalTo(@(column * childLabelWidth+5));
            make.top.equalTo(@(row * childLabelWidth+2));
        }];

    }
}
//- (void)layoutSubviews
//{
//
//    [super layoutSubviews];
//}
- (void)setTitle:(NSString *)title forState:(UIControlState)state
{
    if(self.isOrigin)
    {
        return;
    }
    [super setTitle:title forState:state];
    if (![title isEqualToString:@" "]) {
        for (int i=0; i<9; i++) {
            UILabel *label = (UILabel *)[self viewWithTag:i+1];
            label.hidden = YES;
        }

    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(checkButtonDidBeginClick:)]) {
        [self.delegate checkButtonDidBeginClick:self];
    }
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if ([self.delegate respondsToSelector:@selector(checkButtonDidEndClick:)]) {
        [self.delegate checkButtonDidEndClick:self];
    }
}


@end
